package com.ljp.chat.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljp.chat.entity.pojo.User;
import com.ljp.chat.entity.request.UserInfo;
import com.ljp.chat.exception.WarnException;
import com.ljp.chat.service.UserService;
import org.springframework.web.bind.annotation.*;
import com.ljp.chat.support.enhancement.RestSupportAdapter;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LvJunPeng
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/user")
public class UserController extends RestSupportAdapter<User, UserService> {

    /**
     * Insert user user.
     * 创建账号
     *
     * @param user the user
     * @return the user
     * @throws WarnException the warn exception
     */
    @PostMapping
    public String insertUser(@Valid @RequestBody UserInfo user) throws Exception {
        return service.saveUser(user);
    }

    /**
     * Gets user by page.
     * 用户信息分页查询
     *
     * @param page the page
     * @param user the user
     * @return the user by page
     */
    @GetMapping
    public IPage<User> getUserByPage(Page<User> page, User user) {
        return service.lambdaQuery().page(page);
    }

    /**
     * Update user user.
     * 修改用户信息
     *
     * @param user the user
     * @return the user
     */
    @PutMapping
    public boolean updateUser(@Valid @RequestBody User user) throws Exception {
        return service.updateUser(user);
    }

    /**
     * Delete user by id boolean.
     * 删除用户信息
     *
     * @param ids the ids
     * @return the boolean
     */
    @DeleteMapping
    public Boolean deleteUserById(@RequestParam("id") List<Long> ids) {
        return service.removeBatchByIds(ids);
    }

}
